利用自定义标签,实现select下拉列表默认选中

//创建块函数方法,用于替换文本中的值
function smarty_block_get_cates($params, $content, &$_sm, &$repeat)
{

    if (!$repeat) {
        $ci = &get_instance();
        $cates = $ci->db->get('category')->result_array();

        $cates = get_data($cates);

        $ss = $ci->db->list_fields('category');

        $str = '';

        foreach ($cates as $k => $v) {
            $tmp = $content;
            foreach ($ss as $key => $val) {
                if ($val == 'ctype') {
                    if ($v[$val] == 0)
                        $v[$val] = '频道封面页';
                    if ($v[$val] == 1)
                        $v[$val] = '列表页';
                    if ($v[$val] == 2)
                        $v[$val] = '外链';
                }


                if (isset($params['val'])) {

                    if ($val == 'cname') {
                        if (trim($params['val']) != trim($v[$val])) {
                            $tmp = str_replace('selected', '', $tmp);
                        }
                        $v[$val] = str_repeat('---', $v['level'] * 2) . trim($v[$val]);
                    }

                    $tmp = str_replace("field." . $val, $v[$val], $tmp);
                }
            }

            $str .= $tmp;
        }

        return $str;
    }
}

 

function add_child()
  {
    $cid = $this->uri->segment(4);
    $cate = $this->db->where('id', $cid)->get('category')->row_array();    //根据ID,获取需要被选中的选项名称
    $this->_sm->assign('cate', $cate);
    $this->_sm->display('category/add_child.html');
  }

 

 

前端

<select class="form-select" aria-label="Default select example">
                                            <option value="">顶级栏目</option>
                                            {capture name='me'}
                                            {$cate['cname']}
                                            {/capture}
                                            {get_cates val=$smarty.capture.me}
                                            <option value="field.id" selected>field.cname</option>
                                            {/get_cates}
                                        </select>

 

 

posted @ 2024-06-11 17:27  哆啦啊梦  阅读(1)  评论(0编辑  收藏  举报